//
//  ELVerticalFormLabel.swift
//  WZApp
//
//  Created by evol on 2018/7/30.
//  Copyright © 2018年 evol. All rights reserved.
//

import UIKit

class ELVerticalFormLabel: UIView {

    init(text:String!, font: UIFont = UIFont.systemFont(ofSize: 15), textColor: UIColor = .black, space: CGFloat = 0){
        super.init(frame: .zero)
        self.width = font.pointSize + 1

        var lastTop:CGFloat = 0
        for c in text {
            let textLayer = CATextLayer()
            textLayer.font = font
            textLayer.fontSize = font.pointSize;
            textLayer.string = String(c)
            textLayer.foregroundColor = textColor.cgColor
            textLayer.contentsScale = UIScreen.main.scale
            self.layer.addSublayer(textLayer)
            let attributes:[NSAttributedStringKey: Any] = [NSAttributedStringKey.font: font]
            let size = (String(c) as NSString).boundingRect(with: CGSize(width: 100, height: 100), options: .usesLineFragmentOrigin, attributes: attributes, context: nil).size
            var frame = textLayer.frame;
            frame.size = size;
            textLayer.frame = frame
            if c.isChinese == false { //如果不是汉字 旋转
                textLayer.setAffineTransform(CGAffineTransform(rotationAngle: CGFloat.pi/2))
            }
            var frame1 = textLayer.frame;
            frame1.origin.x = self.width/2 - textLayer.frame.width/2
            frame1.origin.y = lastTop == 0 ? lastTop : lastTop + space - 1
            textLayer.frame = frame1

            lastTop = textLayer.frame.maxY
        }
        self.height = lastTop
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
